<?php

namespace app\common\model\Business;

use think\Model;
use traits\model\SoftDelete;

class Address extends Model
{
    use SoftDelete;
    // 表名
    protected $name = 'business_address';
    // 忽略数据表不存在的字段
    protected $field = true;
    // 设置返回的结果类型
    protected $resultSetType = 'collection';
    // 自动写入时间戳字段
    protected $autoWriteTimestamp = 'int';
    // 定义时间戳字段名
    protected $createTime = 'createtime';
    //禁止写入时间字段
    protected $updateTime = false;
    // 软删除的字段
    protected $deleteTime = 'deletetime';
    // 追加属性
    protected $append = [
        'province_text',
        'city_text',
        'district_text',
        'code',
    ];
    public function business()
    {
        return $this->belongsTo(Business::class,'parentid','id',[],'left')->setEagerlyType(0);
    }
    public function getProvinceTextAttr($value,$data)
    {
        return $data['province']?model('region')->where('code',$data['province'])->find()['name']:'';
    }
    public function getCityTextAttr($value,$data)
    {
        return $data['city']?model('region')->where('code',$data['city'])->find()['name']:'';
    }
    public function getDistrictTextAttr($value,$data)
    {
        return $data['district']?model('region')->where('code',$data['district'])->find()['name']:'';
    }
    public function getCodeAttr($value,$data)
    {
        ($code = $data['district']) || ($code = $data['city']) || ($code = $data['province']);
        return $code;
    }
}
